package com.apes.scm.masterdata.product.model;

/**
 * Created by lishikun on 2021/5/8 20:38
 */

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Objects;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CategoryWorkHours.CONST_TABLE_NAME)
public class CategoryWorkHours extends BaseModel {
    public static final String CONST_TABLE_NAME = "mas_category_work_hours";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "商品类目关联工时")
    @Comment("唯一码")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "category_id", referencedColumnName = "id", nullable = false)
    @Comment("商品类目")
    private Category categoryId;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("工时商品")
    private Product product;

    public Product getProduct() {
        return product;
    }
    public void setProduct(Product product) {
        this.product = product;
    }

    @Transient
    private String category;

    public String getCategory() {
        if (Objects.nonNull(this.categoryId)){
            return categoryId.getId();
        }
        return this.category;
    }

    @Transient
    private String categoryName;

    public String getCategoryName() {
        if (Objects.nonNull(this.categoryId)){
            return categoryId.getName();
        }
        return this.categoryName;
    }

    @Transient
    private String productId;

    public String getProductId() {
        if (Objects.nonNull(this.product)){
            return product.getId();
        }
        return this.productId;
    }

    @Transient
    private String productName;

    public String getProductName() {
        if (Objects.nonNull(this.product)){
            return product.getName();
        }
        return this.productName;
    }
}
